package com.example.gss.controller;

import com.example.gss.entity.Competition;
import com.example.gss.Service.CompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 学科竞赛记录控制器
 */
@RestController
@RequestMapping("/api/competitions")
public class CompetitionController {

    @Autowired
    private CompetitionService competitionService;

    /**
     * 添加竞赛记录
     */
    @PostMapping
    public ResponseEntity<Competition> addCompetition(@RequestBody Competition competition) {
        Competition savedCompetition = competitionService.save(competition);
        return new ResponseEntity<>(savedCompetition, HttpStatus.CREATED);
    }

    /**
     * 获取所有竞赛记录
     */
    @GetMapping
    public ResponseEntity<List<Competition>> getAllCompetitions() {
        List<Competition> competitions = competitionService.findAll();
        return ResponseEntity.ok(competitions);
    }

    /**
     * 根据ID获取竞赛记录
     */
    @GetMapping("/{id}")
    public ResponseEntity<Competition> getCompetitionById(@PathVariable String id) {
        Competition competition = competitionService.findById(id);
        return competition != null ?
                ResponseEntity.ok(competition) :
                ResponseEntity.notFound().build();
    }

    /**
     * 根据学生ID获取竞赛记录
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<Competition>> getCompetitionsByStudentId(@PathVariable String studentId) {
        List<Competition> competitions = competitionService.findByStudentId(studentId);
        return ResponseEntity.ok(competitions);
    }

    /**
     * 更新竞赛记录
     */
    @PutMapping("/{id}")
    public ResponseEntity<Competition> updateCompetition(@PathVariable String id, @RequestBody Competition competition) {
        Competition updatedCompetition = competitionService.update(id, competition);
        return updatedCompetition != null ?
                ResponseEntity.ok(updatedCompetition) :
                ResponseEntity.notFound().build();
    }

    /**
     * 删除竞赛记录
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCompetition(@PathVariable String id) {
        competitionService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 获取指定日期范围内的竞赛记录
     */
    @GetMapping("/date-range")
    public ResponseEntity<List<Competition>> getCompetitionsByDateRange(
            @RequestParam("start") Date startDate,
            @RequestParam("end") Date endDate) {
        List<Competition> competitions = competitionService.findByDateRange(startDate, endDate);
        return ResponseEntity.ok(competitions);
    }

    /**
     * 计算学生的竞赛总积分
     */
    @GetMapping("/student/{studentId}/total-score")
    public ResponseEntity<Double> calculateTotalScore(@PathVariable String studentId) {
        Double totalScore = competitionService.calculateTotalScore(studentId);
        return ResponseEntity.ok(totalScore);
    }
}